﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.line
{
    public partial class addedi : BasePage
    {
        protected TextBox title;
        protected TextBox img;
        protected FileUpload upload;
        protected Image imgPreview;
        protected Button btnPublish;
        protected TextBox price;
        protected Editor introduce;
        protected Button submit;
        protected CheckBox isrecommend;

        protected int id = 0;
        Wjtour.Domain.line entity = new Domain.line();
        line_bll _bll = new line_bll();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            submit.Click += new EventHandler(submit_Click);
            btnPublish.Click += btnPublish_Click;
            if (id > 0)
            {
                entity = _bll.Load(id);
                if (!IsPostBack)
                {
                    if (entity != null)
                    {
                        title.Text = entity.title;
                        img.Text = entity.img;
                        imgPreview.ImageUrl = entity.img;
                        price.Text = entity.price.ToString();
                        introduce.Text = entity.introduce;
                        isrecommend.Checked = entity.isrecommend == 1;
                    }
                }
            }
        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                //string newpath = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 100, 100, "jpg");
                imgPreview.ImageUrl = "/uploads/" + filename;
                img.Text = "/uploads/" + filename;
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }
        

        void submit_Click(object sender, EventArgs e)
        {
            entity.id = id;
            entity.title = title.Text;
            entity.img = img.Text;
            entity.price = Utils.StrToDouble(price.Text, 0);
            entity.introduce = introduce.Text;
            entity.isrecommend = isrecommend.Checked ? 1 : 0;
            if (entity.id == 0)
                entity.addtime = DateTime.Now;

            bool b = _bll.Save(entity);
            if (b)
            {
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }

        }
    }
}